package com.ruoyi.vbze.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.vbze.domain.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.vbze.domain.Plant;
import com.ruoyi.vbze.service.IPlantService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 植株管理Controller
 *
 * @author vbze
 * @date 2023-02-22
 */
@RestController
@RequestMapping("/plant")
public class PlantController extends BaseController {
    @Autowired
    private IPlantService plantService;

    /**
     * 查询植株管理列表
     */
    @RequiresPermissions("vbze:plant:list")
    @GetMapping("/list")
    public TableDataInfo list(Plant plant) {
        startPage();
        List<Plant> list = plantService.selectPlantList(plant);
        return getDataTable(list);
    }

    /**
     * 导出植株管理列表
     */
    @RequiresPermissions("vbze:plant:export")
    @Log(title = "植株管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Plant plant) {
        List<Plant> list = plantService.selectPlantList(plant);
        ExcelUtil<Plant> util = new ExcelUtil<Plant>(Plant.class);
        util.exportExcel(response, list, "植株管理数据");
    }

    /**
     * 获取植株管理详细信息
     */
    @RequiresPermissions("vbze:plant:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(plantService.selectPlantById(id));
    }

    /**
     * 新增植株管理
     */
    @RequiresPermissions("vbze:plant:add")
    @Log(title = "植株管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Plant plant) {
        return toAjax(plantService.insertPlant(plant));
    }

    /**
     * 修改植株管理
     */
    @RequiresPermissions("vbze:plant:edit")
    @Log(title = "植株管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Plant plant) {
        return toAjax(plantService.updatePlant(plant));
    }

    /**
     * 删除植株管理
     */
    @RequiresPermissions("vbze:plant:remove")
    @Log(title = "植株管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(plantService.deletePlantByIds(ids));
    }


    @Log(title = "植株管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("vbze:plant:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Plant> util = new ExcelUtil<Plant>(Plant.class);
        List<Plant> plantList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
//        String message = userService.importUser(fieldList, updateSupport, operName);
        String message = plantService.importPlant(plantList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<Plant> util = new ExcelUtil<Plant>(Plant.class);
        util.importTemplateExcel(response, "植株数据");
    }
}
